﻿using ML.Utils;
using Quartz;
using Quartz.Impl;

namespace ML.Plug
{
    /// <summary>
    /// 定时任务创建调度器和触发器
    /// </summary>
    public class QuartzJobScheduler
    {
        /// <summary>
        /// 创建调度器和触发器
        /// </summary>
        public static void Start()
        {
            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
            scheduler.Start();

            //资金账户数据的汇总
            IJobDetail capitalAccountJob = JobBuilder.Create<CapitalAccountJob>().Build();
            ITrigger capitalaccount_trigger = TriggerBuilder.Create()
              .WithIdentity("capitalaccount_trigger", "chineserestaurant")
              .WithCronSchedule(ConfigHelper.CapitalAccountTimer)
              .Build();
            scheduler.ScheduleJob(capitalAccountJob, capitalaccount_trigger);

            //产品销量汇总
            IJobDetail productJob = JobBuilder.Create<ProductJob>().Build();
            ITrigger product_trigger = TriggerBuilder.Create()
              .WithIdentity("product_trigger", "chineserestaurant")
              .WithCronSchedule(ConfigHelper.ProductTimer)
              .Build();
            scheduler.ScheduleJob(productJob, product_trigger);

            //订单扫描定时完成
            IJobDetail orderJob = JobBuilder.Create<OrderStateJob>().Build();
            ITrigger orderstate_trigger = TriggerBuilder.Create()
              .WithIdentity("orderstate_trigger", "chineserestaurant")
              .WithCronSchedule(ConfigHelper.OrderStateTimer)
              .Build();
            scheduler.ScheduleJob(orderJob, orderstate_trigger);

            //订单扫描定时未支付并取消
            IJobDetail orderCancelJob = JobBuilder.Create<OrderCancelJob>().Build();
            ITrigger orderCance_trigger = TriggerBuilder.Create()
                .WithIdentity("orderCance_trigger", "chineserestaurant")
                .WithCronSchedule(ConfigHelper.OrderCancelTimer)
                .Build();
            scheduler.ScheduleJob(orderCancelJob, orderCance_trigger);
        }
    }
}